package org.ekingstar.qd.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.ekingstar.qd.domain.DeployQdCompareCond;
import org.ekingstar.qd.domain.DeployQdSimplemeasu;
import org.ekingstar.qd.domain.DeployQdWhereClause;
import org.ekingstar.qd.domain.QdCompareCond;
import org.ekingstar.qd.domain.QdDomain;
import org.ekingstar.qd.domain.QdSimplemeasu;
import org.ekingstar.qd.domain.QdWhereClause;
import org.ekingstar.qd.service.CDeployService;

import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.domain.RoleFunction;
import com.ekingstar.framework.security.UserUtil;

/**
 * 主题发布业务处理
 * @author 
 */
public class CDeployServiceImpl implements CDeployService {
	private HibernateTemplate hibernateTemplate;
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@SuppressWarnings("unused")
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/**
	 * @descripte 发布显示字段列，成功返回1，失败返回-1 
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @param strMeasureId 显示字段ID号集
	 * @return int
	 */
	@SuppressWarnings("unchecked")
	public int saveOrUpdateDeployMeasure(String strDomainId,String strRoleId,String[] strMeasureId){
		int retInt = -1;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				//删除数据
				String hql = " from DeployQdSimplemeasu deployQdSimplemeasu where deployQdSimplemeasu.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdSimplemeasu.roleId.id='"+strRoleId.toString().trim()+"'";
				List l = hibernateTemplate.find(hql);
				if(l!=null && l.size()>0){
					for(int i=0;i<l.size();i++){
						DeployQdSimplemeasu deployQdSimplemeasu = (DeployQdSimplemeasu)l.get(i);
						if(deployQdSimplemeasu!=null && deployQdSimplemeasu.getId()!=null && !"".equals(deployQdSimplemeasu.getId().toString().trim())){
							hibernateTemplate.delete(deployQdSimplemeasu);
						}						
					}
				}
				
				//添加数据
				if(strMeasureId!=null && strMeasureId.length>0){
					for(int i=0;i<strMeasureId.length;i++){
						if(strMeasureId[i]!=null && !"".equals(strMeasureId[i].toString().trim())){
							DeployQdSimplemeasu deployQdSimplemeasu = new DeployQdSimplemeasu();
							
							QdDomain qdDomain = new QdDomain();
							qdDomain.setId(strDomainId);
							deployQdSimplemeasu.setTQdDomain(qdDomain);
							
							Role role = new Role();
							role.setId(strRoleId);
							deployQdSimplemeasu.setRoleId(role);
													
							QdSimplemeasu qdSimplemeasu = new QdSimplemeasu();
							qdSimplemeasu.setId(strMeasureId[i].toString().trim());
							deployQdSimplemeasu.setSimplemeasuId(qdSimplemeasu);
							
							deployQdSimplemeasu.setOrderid(Long.parseLong(Integer.toString(i)));
							
							hibernateTemplate.save(deployQdSimplemeasu);						
						}				
					}
				}
				
				retInt = 1;
				
			}
		}catch(Exception e){
			e.printStackTrace();
			retInt = -1;
		}
		
		return retInt;
	}
	
	
	/**
	 * @descripte 查询发布的显示字段列 
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @return List
	 */
	@SuppressWarnings({ "unchecked", "unchecked" })
	public List<QdSimplemeasu> getDeployMeasureList(String strDomainId,String strRoleId){
		List<QdSimplemeasu> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " select deployQdSimplemeasu.simplemeasuId from DeployQdSimplemeasu deployQdSimplemeasu where deployQdSimplemeasu.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdSimplemeasu.roleId.id='"+strRoleId.toString().trim()+"' order by deployQdSimplemeasu.orderid ";
				retList = hibernateTemplate.find(hql);
			}else{
				String hql = " select deployQdSimplemeasu.simplemeasuId from DeployQdSimplemeasu deployQdSimplemeasu where 1<1 ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		
		return retList;
	}
	
	/**
	 * @descripte 根据主题id号和角色ID号查询显示列字段数据信息 
	 * @param strDomainId 主题ID号
	 * @param strRoleId   角色ID号
	 * @return List
	 */
	@SuppressWarnings("unchecked")
	public List<QdSimplemeasu> getMeasureList(String strDomainId,String strRoleId){
		List<QdSimplemeasu> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " from QdSimplemeasu qdSimplemeasu where qdSimplemeasu.TQdDomain.id='"+strDomainId.toString().trim()+"' "
				+" and qdSimplemeasu.id not in( select deployQdSimplemeasu.simplemeasuId.id from DeployQdSimplemeasu deployQdSimplemeasu where deployQdSimplemeasu.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdSimplemeasu.roleId.id='"+strRoleId.toString().trim()+"') order by qdSimplemeasu.orderid ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		return retList;
	}
	
	/**
	 * @descripte 发布查询字段，成功返回1，失败返回-1 
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @param strMeasureId 查询字段ID号集
	 * @return int
	 */
	@SuppressWarnings({ "unchecked" })
	public int saveOrUpdateDeployCompareCond(String strDomainId,String strRoleId,String[] strCompareCondId){
		int retInt = -1;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				//删除数据
				String hql = " from DeployQdCompareCond deployQdCompareCond where deployQdCompareCond.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdCompareCond.roleId.id='"+strRoleId.toString().trim()+"'";
				List l = hibernateTemplate.find(hql);
				if(l!=null && l.size()>0){
					for(int i=0;i<l.size();i++){
						DeployQdCompareCond deployQdCompareCond = (DeployQdCompareCond)l.get(i);
						if(deployQdCompareCond!=null && deployQdCompareCond.getId()!=null && !"".equals(deployQdCompareCond.getId().toString().trim())){
							hibernateTemplate.delete(deployQdCompareCond);
						}						
					}
				}
				
				//添加数据
				if(strCompareCondId!=null && strCompareCondId.length>0){
					for(int i=0;i<strCompareCondId.length;i++){
						if(strCompareCondId[i]!=null && !"".equals(strCompareCondId[i].toString().trim())){
							DeployQdCompareCond deployQdCompareCond = new DeployQdCompareCond();
							
							QdDomain qdDomain = new QdDomain();
							qdDomain.setId(strDomainId);
							deployQdCompareCond.setTQdDomain(qdDomain);
							
							Role role = new Role();
							role.setId(strRoleId);
							deployQdCompareCond.setRoleId(role);
													
							QdCompareCond qdCompareCond = new QdCompareCond();
							qdCompareCond.setId(strCompareCondId[i].toString().trim());
							deployQdCompareCond.setComparecondId(qdCompareCond);
							
							deployQdCompareCond.setOrderid(Long.parseLong(Integer.toString(i)));
							hibernateTemplate.save(deployQdCompareCond);						
						}				
					}
				}				
				
				retInt = 1;				
			}
		}catch(Exception e){
			e.printStackTrace();
			retInt = -1;
		}
		return retInt;
	}
	
	
	/**
	 * @descripte 查询发布的查询字段 
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @return List
	 */
	@SuppressWarnings({ "unchecked", "unchecked" })
	public List<QdCompareCond> getDeployCompareCondList(String strDomainId,String strRoleId){
		List<QdCompareCond> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " select deployQdCompareCond.comparecondId from DeployQdCompareCond deployQdCompareCond where deployQdCompareCond.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdCompareCond.roleId.id='"+strRoleId.toString().trim()+"' order by deployQdCompareCond.orderid";
				retList = hibernateTemplate.find(hql);
			}else{
				String hql = " select deployQdCompareCond.comparecondId from DeployQdCompareCond deployQdCompareCond where 1<1 ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		
		return retList;
	}
	

	/**
	 * @descripte 根据主题id号和角色ID号查询显示列字段数据信息 
	 * @param strDomainId 主题ID号
	 * @param strRoleId   角色ID号
	 * @return List
	 */
	@SuppressWarnings("unchecked")
	public List<QdCompareCond> getCompareCondList(String strDomainId,String strRoleId){
		List<QdCompareCond> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " from QdCompareCond qdCompareCond where qdCompareCond.TQdDomain.id='"+strDomainId.toString().trim()+"' "
				+" and qdCompareCond.id not in( select deployQdCompareCond.comparecondId.id from DeployQdCompareCond deployQdCompareCond where deployQdCompareCond.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdCompareCond.roleId.id='"+strRoleId.toString().trim()+"') ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		return retList;
	}
	
	/**
	 * @descripte 发布过滤条件字段，成功返回1，失败返回-1 
	 * @param strDomainId       主题ID号
	 * @param strRoleId         角色ID号
	 * @param strWhereClauseId  过滤条件字段ID号集
	 * @return int
	 */
	@SuppressWarnings({ "unchecked"})
	public int saveOrUpdateDeployWhereClause(String strDomainId,String strRoleId,String[] strWhereClauseId){
		int retInt = -1;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				//删除数据
				String hql = " from DeployQdWhereClause deployQdWhereClause where deployQdWhereClause.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdWhereClause.roleId.id='"+strRoleId.toString().trim()+"'";
				List l = hibernateTemplate.find(hql);
				if(l!=null && l.size()>0){
					for(int i=0;i<l.size();i++){
						DeployQdWhereClause deployQdWhereClause = (DeployQdWhereClause)l.get(i);
						if(deployQdWhereClause!=null && deployQdWhereClause.getId()!=null && !"".equals(deployQdWhereClause.getId().toString().trim())){
							hibernateTemplate.delete(deployQdWhereClause);
						}						
					}
				}
				
				//添加数据
				if(strWhereClauseId!=null && strWhereClauseId.length>0){
					for(int i=0;i<strWhereClauseId.length;i++){
						if(strWhereClauseId[i]!=null && !"".equals(strWhereClauseId[i].toString().trim())){
							DeployQdWhereClause deployQdWhereClause = new DeployQdWhereClause();
							
							QdDomain qdDomain = new QdDomain();
							qdDomain.setId(strDomainId);
							deployQdWhereClause.setTQdDomain(qdDomain);
							
							Role role = new Role();
							role.setId(strRoleId);
							deployQdWhereClause.setRoleId(role);
													
							QdWhereClause qdWhereClause = new QdWhereClause();
							qdWhereClause.setId(strWhereClauseId[i].toString().trim());
							deployQdWhereClause.setWhereClauseId(qdWhereClause);
							
							deployQdWhereClause.setOrderid(Long.parseLong(Integer.toString(i)));
							hibernateTemplate.save(deployQdWhereClause);						
						}				
					}
				}				
				
				retInt = 1;
				
			}
		}catch(Exception e){
			e.printStackTrace();
			retInt = -1;
		}
		return retInt;
	}
	
	
	/**
	 * @descripte 查询发布的过滤条件字段
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @return List
	 */
	@SuppressWarnings({ "unchecked", "unchecked" })
	public List<QdWhereClause> getDeployWhereClauseList(String strDomainId,String strRoleId){
		List<QdWhereClause> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " select deployQdWhereClause.whereClauseId  from DeployQdWhereClause deployQdWhereClause where deployQdWhereClause.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdWhereClause.roleId.id='"+strRoleId.toString().trim()+"' order by deployQdWhereClause.orderid ";
				retList = hibernateTemplate.find(hql);
			}else{
				String hql = " select deployQdWhereClause.whereClauseId  from DeployQdWhereClause deployQdWhereClause where 1<1 ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		
		return retList;
	}
	
	/**
	 * @descripte 查询过滤条件字段
	 * @param strDomainId  主题ID号
	 * @param strRoleId    角色ID号
	 * @return List
	 */
	@SuppressWarnings("unchecked")
	public List<QdWhereClause> getWhereClauseList(String strDomainId,String strRoleId){
		List<QdWhereClause> retList = null;
		try{
			if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				String hql = " from QdWhereClause qdWhereClause where qdWhereClause.TQdDomain.id='"+strDomainId.toString().trim()+"' "
				+" and qdWhereClause.id not in( select deployQdWhereClause.whereClauseId.id from DeployQdWhereClause deployQdWhereClause where deployQdWhereClause.TQdDomain.id='"+strDomainId.toString().trim()+"' and deployQdWhereClause.roleId.id='"+strRoleId.toString().trim()+"') ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		return retList;
	}
	
	/**
	 * @descripte 发布角色，成功返回1，失败返回-1 
	 * @param functionId    主题Function ID号	
	 * @param strRoleId     角色ID号集
	 * @return int
	 */
	@SuppressWarnings({ "unchecked"})
	public int saveOrUpdateDeployRole(String functionId,String[] strRoleId){
		int retInt = -1;
		try{
			if(functionId!=null && !"".equals(functionId.toString().trim())){
				//删除数据
				String hql = " from RoleFunction roleFunction where roleFunction.TFunction.id='"+functionId.toString().trim()+"' and  roleFunction.deleteDate is null ";
				List l = hibernateTemplate.find(hql);
				if(l!=null && l.size()>0){
					for(int i=0;i<l.size();i++){
						RoleFunction roleFunction = (RoleFunction)l.get(i);
						if(roleFunction!=null && roleFunction.getId()!=null && !"".equals(roleFunction.getId().toString().trim())){
							hibernateTemplate.delete(roleFunction);
						}						
					}
				}
				
				//添加数据
				if(strRoleId!=null && strRoleId.length>0){
					Function function = (Function)hibernateTemplate.get(Function.class, functionId.toString().trim());	;
					Date date = new Date();
					String userId = UserUtil.getCurrentUserId();
					
					for(int i=0;i<strRoleId.length;i++){
						if(strRoleId[i]!=null && !"".equals(strRoleId[i].toString().trim())){
							RoleFunction roleFunction = new RoleFunction();
							roleFunction.setTFunction(function);			
							
							Role role = (Role)hibernateTemplate.get(Role.class, strRoleId[i].toString().trim());							
							roleFunction.setTRole(role);
													
							roleFunction.setAddDate(date);
							roleFunction.setAdderId(userId);						
							
							hibernateTemplate.save(roleFunction);						
						}				
					}
				}				
				
				retInt = 1;
				
			}
		}catch(Exception e){
			e.printStackTrace();
			retInt = -1;
		}
		return retInt;
	}
	
	/**
	 * @descripte 查询发布的角色 
	 * @param functionId  主题Function ID号
	 * @return List
	 */
	@SuppressWarnings({ "unchecked", "unchecked" })
	public List<Role> getDeployRoleList(String functionId){
		List<Role> retList = null;
		try{
			if(functionId!=null && !"".equals(functionId.toString().trim())){
				String hql = " select roleFunction.TRole from RoleFunction roleFunction where roleFunction.TFunction.id='"+functionId.toString().trim()+"' and roleFunction.deleteDate is null and roleFunction.TRole.deleteDate is null ";
				retList = hibernateTemplate.find(hql);
			}else{
				String hql = " select roleFunction.TRole from RoleFunction roleFunction where  1<1 ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		
		return retList;
	}
	
	/**
	 * @descripte 根据主题id号查询角色数据信息 
	 * @param functionId 主题Function ID号
	 * @return List
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getRoleList(String functionId){
		List<Role> retList = null;
		try{
			if(functionId!=null && !"".equals(functionId.toString().trim())){
				String hql = " from Role role where role.deleteDate is null "
				+" and role.id not in(select roleFunction.TRole.id from RoleFunction roleFunction where roleFunction.TFunction.id='"+functionId.toString().trim()+"' and roleFunction.deleteDate is null and roleFunction.TRole.deleteDate is null ) ";
				retList = hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		return retList;
	}
	
	/**
	 * @descripte 根据Function ID号查询角色数据信息 
	 * @param functionId 主题Function ID号
	 * @return List
	 */
	@SuppressWarnings({ "unused", "unchecked" })
	public List<Role> getRoleListByFunctionId(String functionId){
		List<Role> retList = null;
		try{
			if(functionId!=null && !"".equals(functionId.toString().trim())){
				StringBuffer sb = new StringBuffer(" from Role a where a.deleteDate is null  and a.id  in (select roleFunction.TRole.id from RoleFunction roleFunction where roleFunction.TFunction.id='"+functionId.toString().trim()+"' and roleFunction.deleteDate is null and roleFunction.TRole.deleteDate is null )");
				retList = hibernateTemplate.find(sb.toString());
			}			
		}catch(Exception e){
			e.printStackTrace();
			retList = null;
		}
		return retList;
	}
}
